package com.wsoft.hr.pm.manager;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.constant.redis.HrPmRedisKeyConstant;
import com.wsoft.constant.redis.RedisKeyConstant;
import com.wsoft.core.annotation.GetRedisCache;
import com.wsoft.core.service.RedisService;
import com.wsoft.entity.BaseEntity;
import com.wsoft.hr.pm.entity.HrPmGradeEntity;
import com.wsoft.hr.pm.mapper.HrPmGradeMapper;
import com.wsoft.hr.pm.query.grade.HrPmGradeListQuery;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author Z001
 * @Date 2025-03-01 08:35:23
 */
@Service
public class HrPmGradeManager extends ServiceImpl<HrPmGradeMapper, HrPmGradeEntity> {

    @Resource
    private RedisService redisService;

    /**
     * 查询列表
     * @param param
     * @return
     */
    public List<HrPmGradeEntity> list(HrPmGradeListQuery param){
        QueryWrapper<HrPmGradeEntity> query = new QueryWrapper<>();
        //搜索参数
        query.lambda().like(StrUtil.isNotBlank(param.getName()),HrPmGradeEntity::getName,param.getName())
                .eq(null != param.getEnable(),HrPmGradeEntity::getEnable,param.getEnable())
                .in(CollUtil.isNotEmpty(param.getIdList()), BaseEntity::getId, param.getIdList())
                .orderByDesc(BaseEntity::getCreateTime);
        return super.list(query);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetRedisCache(type = HrPmRedisKeyConstant.GRADE_INFO,
            expire_sec = HrPmRedisKeyConstant.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public HrPmGradeEntity getById(Long id){
        return super.getById(id);
    }

    /**
     * 新增
     * @param entity
     * @return
     */
    @Override
    public boolean save(HrPmGradeEntity entity){
        return super.save(entity);
    }

    /**
     * 更新
     * @param entity
     * @return
     */
    @Override
    public boolean updateById(HrPmGradeEntity entity){
        boolean  b =  super.updateById(entity);
        if (b) {
            redisService.del(HrPmRedisKeyConstant.GRADE_INFO + entity.getId());
        }
        return b;
    }


    /**
     * 删除
     * @param id
     * @return
     */
    public boolean removeById(Long id){
        boolean b = super.removeById(id);
        if (b) {
            redisService.del(HrPmRedisKeyConstant.GRADE_INFO + id);
        }
        return b;
    }

}
